package com.richman.controller;

import java.text.SimpleDateFormat;
import java.util.Map;

import com.richman.hg.HGManager;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;
import com.richman.utils.SoundThread;

public class SpecialAutoBetCentralManager {
	private String bettype = "";
	private String updown = "";
	
	
	public int dobetgame(String bettypearg, String updownarg, HGEvent hgevent, int betgold, double specialMinRatio) {
		try {

			bettype = bettypearg;
			updown = updownarg;

			String wtypestr = "";
			
			String typestr = "";
			
			String gnumstr = "";
			
			String strongstr = "";
			
			String gidstr = "";
			
			
			if(bettype.contains("半场")) {
				wtypestr = "H";	
				
				gidstr = hgevent.hgid;
				
				
				
			}else {
				
				gidstr = hgevent.gid;
				
				
			}
			
			
			//滚球大小
			if(bettype.contains("滚球") && bettype.contains("大小")) {
				wtypestr += "R";
			}
			

			
			if(bettype.contains("让球")) {
				
				wtypestr += "R";
				
				if(updown.contains("1")) {
					
					
					
					
					typestr = "H";
					gnumstr = hgevent.gnumh;
					
				}else {
					
					
					typestr = "C";
					gnumstr = hgevent.gnumc;
				}
				
				
				
				
				if(bettype.contains("全场")) {
					
					strongstr = hgevent.strong;
				}else {
					
					strongstr = hgevent.hstrong;
				}
				

				
			}else {
				
				wtypestr += "OU";
				
				if(updown.contains("1")) {
					
					
					typestr = "C";
					gnumstr = hgevent.gnumc;

				}else {
					
					
					typestr = "H";
					gnumstr = hgevent.gnumh;
				}
				
			}
			

			//滚球让球
			if(bettype.contains("滚球") && bettype.contains("让球")) {
				wtypestr += "E";
			}
		
//			double specialMinRatio = DaShuiClient.mainWnd.getSpecialMinRatio();
//			
//			double specialDXQMinRatio = DaShuiClient.mainWnd.getSpecialDXQMinRatio();
			
			for(int i = 0; i < HGManager.redcardsRecords.size(); i++) {
				if(HGManager.redcardsRecords.elementAt(i).gnumh.equals(gnumstr)) {
					if(Integer.parseInt(HGManager.redcardsRecords.elementAt(i).redcardh) > 0) {
						System.out.println(hgevent.homeTeam + "有红牌，不投注");
						return 0;
					}
				}
				
				if(HGManager.redcardsRecords.elementAt(i).gnumc.equals(gnumstr)) {
					if(Integer.parseInt(HGManager.redcardsRecords.elementAt(i).redcardc) > 0) {
						System.out.println(hgevent.awayTeam + "有红牌，不投注");
						return 0;
					}
				}
			}
			
			Map<String, String> oddUpdres = NegativeRatioClient.betCtrMng.updateSpecialAccountRatio(gidstr, typestr, gnumstr, strongstr, wtypestr + typestr, wtypestr);
			

			Double hgratio = Double.parseDouble(oddUpdres.get("ratio"));
			
			
			
			Double hgpankou = Double.parseDouble(oddUpdres.get("pankou"));
			

			
			int hgmaxbetgold = Integer.parseInt(oddUpdres.get("maxbet").replace(",", ""));
			
			//hgmaxbetgold = 600;
			
			//int hgsetbetgold = DaShuiClient.mainWnd.gethgBetAmount();
			
			
			
			
			
			int hgbetgold = betgold;
			if(hgmaxbetgold < hgbetgold) {
				hgbetgold = hgmaxbetgold;
			}
			

			SimpleDateFormat dfSec = new SimpleDateFormat("HH:mm:ss");
			
			if(oddUpdres == null ) {
				
				System.out.println(dfSec.format(System.currentTimeMillis()) + ":获取赔率失败");
				
				return 0;
			}
			
			
			String betres = "";

			
			BetAdyData bad = new BetAdyData();
			
			if(bettype.contains("大小")) {
				if(updownarg == "1") {
					bad.hgbettype = "大" + oddUpdres.get("ratio");
				}else {
					bad.hgbettype = "小" + oddUpdres.get("ratio");
				}
			}else {
				if(updownarg == "1") {
					bad.hgbettype = hgevent.homeTeam + oddUpdres.get("ratio");
				}else {
					bad.hgbettype = hgevent.awayTeam + oddUpdres.get("ratio");
				}
			}
			
			
			
			bad.hgps38 = updownarg;
			
			bad.currenttime = dfSec.format(System.currentTimeMillis());
			
			bad.league = hgevent.leagueName;
			bad.teams = hgevent.homeTeam + "-vs-" + hgevent.awayTeam;
			bad.time = hgevent.eventTime;
			
			bad.betpankou = oddUpdres.get("pankou");
			
			
			
			bad.score = hgevent.homeTeamScore + ":" + hgevent.awayTeamScore;
			
			bad.hgeventid = hgevent.eventId;
			
			bad.bettype = bettype;
			
			
			
			bad.hgbettime = Long.toString(System.currentTimeMillis());
			
			bad.hgbetres = "";
			
			bad.betAmount = "";
			bad.gid = gidstr;
			bad.hgbetgold = Integer.toString(hgbetgold);
			
			double hgratio1 = hgratio;
			if(hgratio < 0) {
				hgratio1 = 2.0 + hgratio;
			}
			
			if(hgratio1 < specialMinRatio) {
				System.out.println(hgevent.homeTeam + ":投注水位低于最低水位," + Double.toString(hgratio1));
				bad.hgbetres = "赔率不符条件";
				NegativeRatioClient.betCtrMng.updateSpecialBetLogData(bad);
				return 0;
			}


			bad.wagerId = "";
			bad.ps38betres = "";
			//bad.ps38bettime = dfSec.format(System.currentTimeMillis());
			
			bad.ps38rebetflag = "0";

		
			//int hgminbetgold = Integer.parseInt(oddUpdres.get("minbet"));
			int hgminbetgold = Integer.parseInt(oddUpdres.get("minbet").replace(",", ""));
			//int hgmaxbetgold = Integer.parseInt(oddUpdres.get("maxbet").replace(",",""));
			
			
			if(hgbetgold < hgminbetgold || hgbetgold > hgmaxbetgold) {
				System.out.println("金额不对");
				return 0;
			}
			
			
			
			

			Map<String,String> res = NegativeRatioClient.betCtrMng.hgdospecialbet(oddUpdres.get("betParam"), hgbetgold);
			

			if(res == null) {
				System.out.println("auto bet: 皇冠账号尚未登录");
				
				System.out.println(dfSec.format(System.currentTimeMillis())  + ": 皇冠账号尚未登录,投注返回null:" + bad.toString());
				bad.hgbetres = "账号掉线";
				NegativeRatioClient.betCtrMng.updateSpecialBetAdyData(bad);
				
				return 0;
				
				//messagelb.setForeground(new Color(255,0,0));
			}else {
				
				if(res.get("status").equals("1") || res.get("status").equals("0")) {
					

					
					bad.league = hgevent.leagueName;
					bad.teams = hgevent.homeTeam + "-vs-" + hgevent.awayTeam;
					bad.time = hgevent.eventTime;
					
					
					
					
					bad.hgeventid = hgevent.eventId;
					
					bad.bettype = bettype;
					
					bad.hgbettype = res.get("betteam") + "@ " + res.get("betratio");
					bad.betpankou = res.get("betpankou");
					bad.hgbettime = bad.hgbettime = Long.toString(System.currentTimeMillis());
					
					bad.hgbetres = res.get("status");
					
					bad.betAmount = res.get("ticketid");
					bad.gid = gidstr;
					bad.hgbetgold = Integer.toString(hgbetgold);
					
					
					
					
					
					double bethgratio = Double.parseDouble(res.get("betratio"));
					if(bethgratio < 0) {
						bethgratio = 2.0 + bethgratio;
					}
					
					NegativeRatioClient.betCtrMng.updateSpecialBetLogData(bad);
					NegativeRatioClient.betCtrMng.updateSpecialBetAdyData(bad);
					//todo remove 测试切换账号投注
//					if(res.get("status").equals("1")) {
//						DaShuiClient.betCtrMng.useNextSpecialAccount();
//					}
					
					
					return hgbetgold;
					

					}else {
						
						if(res.get("status").equals("-4")) {
							//使用下一个特殊账号
							NegativeRatioClient.betCtrMng.useNextSpecialAccount();
						}
						
						
						bad.hgbetres = res.get("status");
						NegativeRatioClient.betCtrMng.updateSpecialBetLogData(bad);
						
						return 0;
						//投注失败
						
					}

				}
				

			
		}catch(Exception e) {
			
			e.printStackTrace();
			return 0;
			
		}
	}
}
